package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JOptionPane;

import contacts614.po.Room;
import contacts614.po.Student;
import service.StudentService;
import view.StudentDialog;
import view.StudentFrame;

public class StudentControllerListener implements ActionListener {
	private StudentFrame studentFrame;
	private StudentDialog studentDialog;
	private StudentService studentService;

	/**
	 * StudentControllerListener的无参构造方法
	 */
	public StudentControllerListener() {
		super();
		studentService = new StudentService();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// 打开新增对话框
		if ("addStudentDialog".equals(e.getActionCommand())) {
			studentDialog = new StudentDialog(studentFrame, "添加学生", null);
		}
		// 新增提交
		else if ("addPost".equals(e.getActionCommand())) {
			Student newStudent = this.getStudentInstance();
			if (newStudent != null) {
				studentService.addStudent(newStudent);
				JOptionPane.showMessageDialog(studentFrame, "添加" + newStudent.getStudentNo() + "学生成功!");
				studentDialog.dispose();
				studentFrame.refresh(studentService.findAllStudent());
			} else {
				JOptionPane.showMessageDialog(studentFrame, "添加学生失败");
			}
		}
		// 打开查询对话框
		else if ("searchStudentDialog".equals(e.getActionCommand())) {
			List<Student> result = new ArrayList<Student>();
			String str = JOptionPane.showInputDialog(studentFrame, "输入学生学号");
			if (str != null && !"".equals(str)) {
				if (str.matches("^[0-9]*$")) {
					String studentNo = str;
					result.add(studentService.getStudent(studentNo));
					studentFrame.showSearchResult(result);

				} else {
					JOptionPane.showMessageDialog(studentFrame, "输入的不是数字", "输入错误", JOptionPane.ERROR_MESSAGE);
				}
			}
		}
		// 打开修改对话框
		else if ("updateStudentDialog".equals(e.getActionCommand())) {
			int selRow = studentFrame.getSelRow();
			if (selRow < 0) {
				JOptionPane.showMessageDialog(studentFrame, "请选择一行数据!");
			} else {
				Student student = studentService.findAllStudent().get(studentFrame.getSelRow());
				studentDialog = new StudentDialog(studentFrame, "修改学生", student);
			}
		}
		// 修改提交
		else if ("updatePost".equals(e.getActionCommand())) {
			Student updatedStudent = this.getStudentInstance();// 实例
			if (updatedStudent != null) {
				studentService.updateStudent(updatedStudent);// 少了getSelow();
				JOptionPane.showMessageDialog(studentFrame, updatedStudent.getStudentNo() + "的学生修改成功!");

				studentDialog.dispose();
				studentFrame.refresh(studentService.findAllStudent());
			} else {
				JOptionPane.showMessageDialog(studentFrame, "更新学生失败!");
			}
		}
		// 删除学生操作
		else if ("deleteStudent".equals(e.getActionCommand())) {
			int selRow = studentFrame.getSelRow();
			String studentNo = studentFrame.getStudentNo();
			if (selRow < 0) {
				JOptionPane.showMessageDialog(studentFrame, "请选择一行数据!");
			} else {
				studentService.deleteStudent(studentNo);
				JOptionPane.showMessageDialog(studentFrame, "删除" + studentNo + "学号学生成功!");
				studentFrame.refresh(studentService.findAllStudent());
			}
		}
		// 刷新学生列表窗口
		else if ("refreshStudentFrame".equals(e.getActionCommand())) {
			studentFrame.refresh(studentService.findAllStudent());
		}
	}

	/**
	 * 得到学生实例
	 * 
	 * @return学生信息
	 */
	private Student getStudentInstance() {
		String studentNo = null;
		String studentName = null;
		Room room = null;
		Date studentBirth = null;
		String studentTelephone = null;
		String studentEmail = null;
		String studentAddress = null;
		Student student = new Student();
		String studentNoInput = studentDialog.getStudentNoTextField().getText().trim();

		if (studentNoInput != null && !"".equals(studentNoInput)) {

			studentNo = studentNoInput;
		} else {
			JOptionPane.showMessageDialog(studentFrame, "studentNo不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		String studentNameInput = studentDialog.getStudentNameTextField().getText().trim();
		if (studentNameInput != null && !"".equals(studentNameInput)) {

			studentName = studentNameInput;
		} else {
			JOptionPane.showMessageDialog(studentFrame, "studentName不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		String roomInput = studentDialog.getRoomNoTextField().getText().trim();
		if (roomInput != null && !"".equals(roomInput)) {
			room = new Room();
			room.setRoomNo(roomInput);

			student.setRoom(room);
		} else {
			JOptionPane.showMessageDialog(studentFrame, "room不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		String sBI = studentDialog.getStudentBirthTextField().getText().trim();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date studentBirthInput = null;
		try {
			studentBirthInput = simpleDateFormat.parse(sBI);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 不能在try里赋值，不然在catch里相当于没赋值
		if (sBI != null && !"".equals(sBI)) {

			studentBirth = studentBirthInput;
		} else {
			JOptionPane.showMessageDialog(studentFrame, "studentBirth不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		String studentTelephoneInput = studentDialog.getStudentTelephoneTextField().getText().trim();
		if (studentTelephoneInput != null && !"".equals(studentTelephoneInput)) {

			studentTelephone = studentTelephoneInput;
		} else {
			JOptionPane.showMessageDialog(studentFrame, "studentTelephone不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		String studentEmailInput = studentDialog.getStudentEmailTextField().getText().trim();

		if (studentEmailInput != null && !"".equals(studentEmailInput)) {

			studentEmail = studentEmailInput;
		} else {
			JOptionPane.showMessageDialog(studentFrame, "studentEmail不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		String studentAddressInput = studentDialog.getStudentAddressTextField().getText().trim();
		if (studentAddressInput != null && !"".equals(studentAddressInput)) {

			studentAddress = studentAddressInput;
		} else {
			JOptionPane.showMessageDialog(studentFrame, "studentAddress不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		if (studentNo != null && studentName != null && room != null && studentBirth != null && studentTelephone != null
				&& studentEmail != null && studentAddress != null) {
			student = new Student(studentNo, studentName, room, studentBirth, studentTelephone, studentEmail,
					studentAddress);
		}
		return student;
	}

	public StudentFrame getStudentFrame() {
		return studentFrame;
	}

	public void setStudentFrame(StudentFrame studentFrame) {
		this.studentFrame = studentFrame;
	}

	public StudentDialog getStudentDialog() {
		return studentDialog;
	}

	public void setStudentDialog(StudentDialog studentDialog) {
		this.studentDialog = studentDialog;
	}

	public StudentService getStudentService() {
		return studentService;
	}

	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}

}
